package com.atguigu.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.atguigu.commonutils.RandomUtil;
import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.ConstantPropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class MsmServiceImpl implements MsmService {

    /*springboot整合的redis*/
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /*发送验证码*/
    @Override
    public boolean sendSms(String phone) {
        String phoneKey="redis:code:"+phone;
        /*1.从redis里面取出数据，看是否可以取到*/
        String redisCode = redisTemplate.opsForValue().get(phoneKey);
        String code = null;
        if(StringUtils.isEmpty(redisCode)){
            /*2.redis里面没有数据，调用阿里云的短信服务*/
            Config config = new Config().setAccessKeyId(ConstantPropertiesUtil.ACCESSKEYID)
                    .setAccessKeySecret(ConstantPropertiesUtil.ACCESSKEYSECRET);
            config.endpoint=ConstantPropertiesUtil.ENDPOINT;
            /*2.根据config创建client*/
            Client client = null;
            try {
                client = new Client(config);
            } catch (Exception e) {
                e.printStackTrace();
            }
            code = RandomUtil.getSixBitRandom();
            HashMap<String, Object> map = new HashMap<>();
            map.put("code",code);
            System.out.println("生成的验证码是："+code);
            String res = JSONObject.toJSONString(map);
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest =
                    new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                            .setPhoneNumbers(phone)
                            .setSignName("阿里云短信测试")/*因为这里读取的时候从配置文件里面读取出来的需要汉化*/
                            .setTemplateCode(ConstantPropertiesUtil.TEMPLATECODE)
                            .setTemplateParam(res);
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            try {
                // 复制代码运行请自行打印 API 的返回值
                client.sendSmsWithOptions(sendSmsRequest, runtime);
            } catch (TeaException error) {
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
                return false;
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                // 如有需要，请打印 error
                com.aliyun.teautil.Common.assertAsString(error.message);
                return false;
            }
        }
        /*3.将生成的短信验证码存放到redis里面，并且设置过期时间*/
        redisTemplate.opsForValue().set(phoneKey,code,60,TimeUnit.SECONDS);
        return true;
    }
}